home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / Apple Event Registry / AE Suites Under Development / Word Services SDK 1.0 / Writeswell Jr. Source / Prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-25  |  8.8 KB  |  464 lines  |  [TEXT/KAHL]

  1. /* Prefs.c
  2.  * Preferences file management
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 18 Apr 92 Mike Crawford
  14.  */
  15.  
  16. #include <EPPC.h>
  17. #include <AppleEvents.h>
  18. #include <AEObjects.h>
  19. #include <Folders.h>
  20. #include "AERegistry.h"
  21. #include "TBConstants.h"
  22. #include "TBGlobals.h"
  23. #include "Prefs.h"
  24. #include "MyGestalt.h"
  25. #include "MyFolders.h"
  26. #include "Gripe.h"
  27.  
  28. Boolean OpenPrefFile( void )
  29. {
  30.     short            oldDrive;
  31.     long            oldDir;
  32.     OSErr            err;
  33.     short            vRef;
  34.     long            dirID;
  35.     WDParam            wPB;
  36.     short            curFile;
  37.     Str255            prefsFileName;
  38.  
  39.     /* Find the preferences folder */
  40.     
  41.     if ( GetPrefFolder( &vRef, &dirID ) )
  42.         return false;
  43.     
  44.     /* Check if file exists */
  45.     
  46.     GetIndString( prefsFileName, kMiscStringID, kPreferencesFileNameStr );
  47.     
  48.     if ( prefsFileName[0] == '\0' )
  49.         return false;
  50.  
  51.     if ( !FileExistsWithThisType( vRef, dirID, prefsFileName, 'pref' ) ){
  52.     
  53.         /* if not, create it with default prefs */
  54.         if ( !CreateDefaultPrefFile( vRef, dirID, prefsFileName ) )
  55.             return false;
  56.     }    
  57.  
  58.     /* otherwise, open the file */
  59.     
  60.     wPB.ioCompletion = 0;
  61.     wPB.ioNamePtr = (StringPtr)NULL;
  62.     
  63.     err = PBHGetVol( &wPB, false );
  64.     if ( err )
  65.         return false;
  66.     
  67.     oldDrive = wPB.ioWDVRefNum;
  68.     oldDir = wPB.ioWDDirID;
  69.  
  70.     wPB.ioVRefNum = vRef;
  71.     wPB.ioWDDirID = dirID;
  72.     wPB.ioNamePtr = (StringPtr)NULL;
  73.         
  74.     err = PBHSetVol( &wPB, false );
  75.     if ( err )
  76.         return false;
  77.     
  78.     curFile = CurResFile();
  79.  
  80.     gPrefFileRefNum = OpenResFile( prefsFileName );
  81.     
  82.     err = ResError();
  83.  
  84.     UseResFile( curFile );            /* Restore old res file */
  85.  
  86.     wPB.ioVRefNum = oldDrive;
  87.     wPB.ioWDDirID = oldDir;
  88.  
  89.     err = PBHSetVol( &wPB, false );            /* Restore original volume */
  90.  
  91.     if ( gPrefFileRefNum == -1 || err ){
  92.         return false;
  93.     }
  94.  
  95.     if ( ValidatePrefsFile() )
  96.         return false;
  97.  
  98.     return true;
  99. }
  100.  
  101. OSErr ValidatePrefsFile( void )
  102. {
  103.     short    curFile;
  104.     short    i;
  105.     OSErr    err;
  106.     Handle    h;
  107.  
  108.     curFile = CurResFile();
  109.     UseResFile( gPrefFileRefNum );
  110.  
  111.     /* Get defaults out of the application */
  112.     
  113.     if ( !( h = Get1Resource( 'PreF', kPFPrefsID )) )
  114.         if ( err = CopyResource( kAFPrefsID, kPFPrefsID, 'PreF', gAppFileRefNum, gPrefFileRefNum  )){
  115.             UseResFile( curFile );
  116.             return err;
  117.         }
  118.  
  119.     UpdateResFile( gPrefFileRefNum );
  120.     
  121.     UseResFile( curFile );    
  122.     
  123.     err = ResError();
  124.     
  125.     return err;
  126. }
  127.  
  128. OSErr GetPrefFolder( short *vRefPtr, long *dirIDPtr )
  129. {
  130.     OSErr    errCode;
  131.     long    blessedID;
  132.  
  133.     if ( FindFolderPresent() ){
  134.         errCode = FindFolder( kOnSystemDisk,
  135.                             kPreferencesFolderType,
  136.                             true,
  137.                             vRefPtr,
  138.                             dirIDPtr );
  139.         return errCode;
  140.     }
  141.     
  142.     /* System 6 style */
  143.  
  144.     /* First, get the refNum of the boot drive, and the ID of the system folder */
  145.     
  146.     errCode = GetSysVolID( vRefPtr, &blessedID );
  147.     if ( errCode ){
  148.         return errCode;
  149.     }
  150.     
  151.     /* Now get the ID of the "Preferences" folder in the system folder.
  152.      * if it does not exist, create it. */
  153.     
  154.     errCode = FindDirectory( *vRefPtr, blessedID, "\pPreferences", dirIDPtr );
  155.     if ( errCode == dirNFErr ){
  156.             errCode = MakeFolder( dirIDPtr, *vRefPtr, blessedID, "\pPreferences" );
  157.             if ( errCode )
  158.                 return errCode;
  159.     } else if ( errCode ){
  160.             return errCode;
  161.     }
  162.     
  163.     return noErr;
  164. }
  165.  
  166. Boolean FileExistsWithThisType( short vRef, long dirID, StringPtr fileName, OSType type )
  167. {
  168.     OSErr            err;
  169.     HParmBlkPtr        pbPtr;
  170.     Boolean            result;
  171.     long            oldSize;
  172.     
  173.     pbPtr = (HParmBlkPtr)NewPtr( sizeof( HParamBlockRec ) );
  174.  
  175.     if ( !pbPtr )
  176.         return false;
  177.         
  178.     pbPtr->fileParam.ioCompletion = (ProcPtr)NULL;
  179.     pbPtr->fileParam.ioDirID = dirID;
  180.     pbPtr->fileParam.ioNamePtr = fileName;
  181.     pbPtr->fileParam.ioVRefNum = vRef;
  182.     pbPtr->fileParam.ioFDirIndex = 0;
  183.     
  184.     err = PBHGetFInfo( pbPtr, false );
  185.     
  186.     if ( err ){
  187.         result = false;
  188.     } else {
  189.     
  190.         if ( pbPtr->fileParam.ioFlFndrInfo.fdType != type )
  191.             result = false;
  192.         else
  193.             result = true;
  194.     }
  195.     
  196.     DisposPtr( pbPtr );
  197.     return result;
  198. }
  199.  
  200. Boolean CreateDefaultPrefFile( short vRef, long dirID, StringPtr fileName )
  201. {
  202.     OSErr            err;
  203.     HParmBlkPtr        pbPtr;
  204.     Boolean            result;
  205.     short            wdRef;
  206.     long            oldDir;
  207.     short            oldVol;
  208.     long            oldSize;
  209.  
  210.     /* We cannot count on the presence of HCreateResFile */
  211.  
  212.     
  213.     pbPtr = (HParmBlkPtr)NewPtr( sizeof( HParamBlockRec ) );
  214.     
  215.     if ( !pbPtr )
  216.         return false;
  217.     
  218.     pbPtr->wdParam.ioCompletion = (ProcPtr)NULL;
  219.     pbPtr->wdParam.ioWDDirID = dirID;
  220.     pbPtr->wdParam.ioNamePtr = (StringPtr)NULL;
  221.     pbPtr->wdParam.ioVRefNum = vRef;
  222.     pbPtr->wdParam.ioWDProcID = 'ERIK';
  223.     
  224.     err = PBOpenWD( pbPtr, false );
  225.  
  226.     if ( err ){
  227.         DisposPtr( pbPtr );
  228.         return false;
  229.     }
  230.     
  231.     wdRef = pbPtr->wdParam.ioVRefNum;
  232.     
  233.     pbPtr->wdParam.ioCompletion = (ProcPtr)NULL;
  234.  
  235.     err = PBHGetVol( pbPtr, false );
  236.     
  237.     if ( err ){
  238.         DisposPtr(  pbPtr );
  239.         return false;
  240.     }
  241.     
  242.     oldDir = pbPtr->wdParam.ioWDDirID;
  243.     oldVol = pbPtr->wdParam.ioWDVRefNum;
  244.     
  245.     ((ParmBlkPtr)pbPtr)->fileParam.ioCompletion = (ProcPtr)NULL;
  246.     ((ParmBlkPtr)pbPtr)->fileParam.ioNamePtr = (StringPtr)NULL;
  247.     ((ParmBlkPtr)pbPtr)->fileParam.ioVRefNum = wdRef;
  248.     
  249.     err = PBSetVol( pbPtr, false );
  250.  
  251.     if ( err ){
  252.         DisposPtr( pbPtr );
  253.         return false;
  254.     }
  255.     
  256.     CreateResFile( fileName );
  257.     
  258.     err = ResError();
  259.     
  260.     if ( err ){
  261.         DisposPtr( pbPtr );
  262.         return false;
  263.     }
  264.  
  265.     pbPtr->fileParam.ioCompletion = (ProcPtr)NULL;
  266.     pbPtr->fileParam.ioDirID = dirID;
  267.     pbPtr->fileParam.ioNamePtr = fileName;
  268.     pbPtr->fileParam.ioVRefNum = vRef;
  269.     pbPtr->fileParam.ioFDirIndex = 0;
  270.     
  271.     err = PBHGetFInfo( pbPtr, false );
  272.     
  273.     if ( err ){
  274.         DisposPtr( pbPtr );
  275.         return false;
  276.     }
  277.  
  278.     pbPtr->fileParam.ioFlFndrInfo.fdType = 'pref';
  279.     pbPtr->fileParam.ioFlFndrInfo.fdCreator = 'MiKe';
  280.     pbPtr->fileParam.ioDirID = dirID;
  281.     pbPtr->fileParam.ioNamePtr = fileName;
  282.     pbPtr->fileParam.ioVRefNum = vRef;
  283.     pbPtr->fileParam.ioFDirIndex = 0;
  284.     
  285.     err = PBHSetFInfo( pbPtr, false );
  286.     
  287.     if ( err ){
  288.         DisposPtr( pbPtr );
  289.         return false;
  290.     }
  291.     
  292.     pbPtr->wdParam.ioCompletion = (ProcPtr)NULL;
  293.     pbPtr->wdParam.ioWDDirID = oldDir;
  294.     pbPtr->wdParam.ioNamePtr = (StringPtr)NULL;
  295.     pbPtr->wdParam.ioVRefNum = oldVol;
  296.  
  297.     err = PBHSetVol( pbPtr, false );
  298.  
  299.     if ( err ){
  300.         DisposPtr( pbPtr );
  301.         return false;
  302.     }
  303.  
  304.     pbPtr->wdParam.ioCompletion = (ProcPtr)NULL;
  305.     pbPtr->wdParam.ioVRefNum = wdRef;
  306.     
  307.     err = PBCloseWD( pbPtr, false );
  308.     
  309.     DisposPtr( pbPtr );
  310.     
  311.     if ( err )
  312.         return false;
  313.     
  314.     return true;
  315. }
  316.  
  317. OSErr CopyResource( short fromID,
  318.                     short toID,
  319.                     ResType theType,
  320.                     short fromFile,
  321.                     short toFile )
  322. {
  323.     short    curFile;
  324.     Handle    h;
  325.     Str255    name;
  326.     short    attr;
  327.     ResType    myType;
  328.     short    myID;
  329.     OSErr    err;
  330.     
  331.     /* This function will not work correctly if the ChangedResource
  332.      * or purge bits are set.
  333.      */
  334.     curFile = CurResFile();
  335.     UseResFile( fromFile );
  336.     
  337.     if ( err = ResError() ){
  338.         return err;
  339.     }
  340.     
  341.     h = GetResource( theType, fromID );
  342.     
  343.     UseResFile( curFile );
  344.     
  345.     if ( err || (err = ResError()) ){
  346.         return err;
  347.     }
  348.     
  349.     if ( !h || !*h ){
  350.         return resNotFound;
  351.     }
  352.     
  353.     GetResInfo( h, &myID, &myType, name );
  354.     if ( err = ResError() ){
  355.         return err;
  356.     }
  357.     attr = GetResAttrs( h );
  358.     if ( err = ResError() ){
  359.         return err;
  360.     }
  361.     
  362.     DetachResource( h );
  363.     if ( err = ResError() ){
  364.         return err;
  365.     }
  366.     
  367.     curFile = CurResFile();
  368.     UseResFile( toFile );
  369.  
  370.     AddResource( h, theType, toID, name );
  371.     if ( err = ResError() ){
  372.         UseResFile( curFile );
  373.         return err;
  374.     }
  375.  
  376.     WriteResource( h );
  377.     if ( err = ResError() ){
  378.         UseResFile( curFile );
  379.         return err;
  380.     }
  381.     
  382.     UseResFile( curFile );
  383.  
  384.     if ( err = ResError() ){
  385.         return err;
  386.     }
  387.     
  388.     SetResAttrs( h, attr );
  389.     if ( err = ResError() ){
  390.         return err;
  391.     }
  392.     
  393.  
  394.     return noErr;
  395. }
  396.  
  397. #ifdef NEVER                        /* This will return in a future version */
  398. void ToggleSelectCheck( void )
  399. {
  400.     WWJrPrefsHdl    prefHdl;
  401.     
  402.     prefHdl = GetPrefHandle();
  403.     if ( !prefHdl ){
  404.         Gripe( "\pCannot get preferences handle" );
  405.         return;
  406.     }
  407.  
  408.     if ( (*prefHdl)->checkSel ){
  409.         (*prefHdl)->checkSel = 0;
  410.     }else{
  411.         (*prefHdl)->checkSel = 1;
  412.     }
  413.  
  414.     ChangedResource( prefHdl );
  415.     WriteResource( prefHdl );
  416.  
  417.     CheckSelectMenu( prefHdl );
  418.  
  419.     return;
  420. }
  421.  
  422. void CheckSelectMenu( WWJrPrefsHdl prefHdl )
  423. {
  424.     MenuHandle    servMenu;
  425.  
  426.     servMenu = GetServiceMenu();
  427.     if ( !servMenu ){
  428.         Gripe( "\pCannot get service menu handle" );
  429.         return;
  430.     }
  431.  
  432.     if ( (*prefHdl)->checkSel ){
  433.         CheckItem( servMenu, kSMCheckSel, true );
  434.     }else{
  435.         CheckItem( servMenu, kSMCheckSel, false );
  436.     }
  437.  
  438.     return;
  439. }
  440. #endif
  441.  
  442. MenuHandle GetServiceMenu( void )
  443. {
  444.     MenuHandle servMenu;
  445.     
  446.     servMenu = (MenuHandle)GetResource( 'MENU', kServMenuID );
  447.     
  448.     return servMenu;
  449. }
  450.  
  451. WWJrPrefsHdl GetPrefHandle( void )
  452. {
  453.     WWJrPrefsHdl    prefHdl;
  454.     short            curFile;
  455.     
  456.     curFile = CurResFile();
  457.     UseResFile( gPrefFileRefNum );
  458.     
  459.     prefHdl = (WWJrPrefsHdl)GetResource( 'PreF', kPFPrefsID );
  460.     
  461.     UseResFile( curFile );
  462.  
  463.     return prefHdl;                    /* Might be Nil; up to caller to check */
  464. }